<?php

class M_public_function extends CI_Model{
	/**
	 * Class	: M_public_function
	 *  
	 * @author Eko Junaidi Salam
	 */
	
	function M_public_function(){
		parent::__construct();
	}
	
	function Copy_Table($oldT,$newT){
		$cek = $this->db->table_exists($newT);
		if (!$cek)
		{
			$sql = "CREATE TABLE $newT LIKE $oldT";
			$rs = $this->db->query($sql);
			$rs = $this->db->query("INSERT INTO $newT (SELECT * FROM $oldT)");
		} 
	}
	
	function GenCode($jenis){
		$data = new stdClass();
		$data->NOMOR_PO = $this->GenCodePo($jenis);
		$data->NOMOR_LPB = $this->GenCodeTerima($jenis);
		$data->NOMOR_DO = $this->GenCodeDO($jenis);
		$data->NOMOR_INVOICE = $this->GenCodeINV($jenis);
		$data->NOMOR_QUOTE = $this->GenCodePenawaran($jenis);
		$data->NOMOR_FAKTUR = $this->GenCodeFPajak($jenis);
		
		return $data;
	}
	
	function GenCodeFPajak($jenis){
		// Nomor Faktur
		// Nomor Faktur : 010.901-13.65203618
		
		$jenis 		= strtolower($jenis);		
		$code = $this->db->where(array('JENIS' => 'T'))->get('MSTFAKTURPAJAK')->result();
		
		if($jenis == "fpajak")
		{
			$rs = $code[0]->KODE_TRANSAKSI.$code[0]->STATUS.".".$code[0]->CABANG."-".$code[0]->TAHUN.".";
			$no = intval(trim($code[0]->URUT_TERAKHIR));
			return $rs.$no;
		}
	}
	
	function GenCodePenawaran($jenis){
		// Penawaran
		// BBAAA/BBBB/SKJF/TTTT			-> AAA = NO URUT, BBBB = BULAN DLM ROMAWI, TTTT => TAHUN, 
		//BB010/VII/SKJF/2012
		
		$jenis 		= strtolower($jenis);
		$code		= "BB";
		
		$bln = mdate("%m", time());
		
		// ------------------------------------------------ Reset Tiap Bulan untuk Generator -----------------	
		// $febs = ($d->th%100 > 0 ? ($d->th%4 == 0 ? ($d->th%400 > 0 ? 1:0):0):0)+28;
		$sekarang = mdate("%Y%m", time());
		
		$historyP = $this->db->where(array('param' => 'PENAWARAN'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$s = new StdClass();
		$s->th = mdate("%Y", time());
		$s->bln = mdate("%m", time());
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubah = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','PENAWARAN')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','PENAWARAN')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		// ---------------------------------------------------------------------------------------------------
		
		switch ($bln)
		{
		case "01":$bln="I/";break;
		case "02":$bln="II/";break;
		case "03":$bln="IIII/";break;
		case "04":$bln="IV/";break;
		case "05":$bln="V/";break;
		case "06":$bln="VI/";break;
		case "07":$bln="VII/";break;
		case "08":$bln="VIII/";break;
		case "09":$bln="IX/";break;
		case "10":$bln="X/";break;
		case "11":$bln="XI/";break;
		case "12":$bln="XII/";break;
		default:
		  $bln = "BBBB/";
		}
		
		$th = mdate("/%Y", time());
		
		if($jenis == "penawaran")
		{
			$rs = $this->db->where(array('param' => 'PENAWARAN'))->get('init')->result();
			$no = intval(trim($rs[0]->value));
			$n = substr("000".$no,-3);
			return $code.$n."/".$bln."SKJF".$th;
		}
	}
	
	function GenCodePo($jenis){
		// PO LOKAL
		// PO/AAA/BBBB/TT/B			-> AAA = NO URUT, BBBB = BULAN DLM ROMAWI, TT => TAHUN, 
		//PO/001/VIII/13/B
		
		// PO IMPORT
		// PO AAA/BBBB/SKJF/TTTT
		
		$jenis 		= strtolower($jenis);
		$codel		= "PO/";
		$codei		= "PO ";
		
		$bln = mdate("%m", time());
		// ------------------------------------------------ Reset Tiap Bulan untuk Generator -----------------	
		// $febs = ($d->th%100 > 0 ? ($d->th%4 == 0 ? ($d->th%400 > 0 ? 1:0):0):0)+28;
		$sekarang = mdate("%Y%m", time());
		
		$historyP = $this->db->where(array('param' => 'POL'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$s = new StdClass();
		$s->th = mdate("%Y", time());
		$s->bln = mdate("%m", time());
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahPOL = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','POL')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','POL')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'POI'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahPOI = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','POI')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','POI')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		// ---------------------------------------------------------------------------------------------------
		
		switch ($bln)
		{
		case "01":$bln="I/";break;
		case "02":$bln="II/";break;
		case "03":$bln="IIII/";break;
		case "04":$bln="IV/";break;
		case "05":$bln="V/";break;
		case "06":$bln="VI/";break;
		case "07":$bln="VII/";break;
		case "08":$bln="VIII/";break;
		case "09":$bln="IX/";break;
		case "10":$bln="X/";break;
		case "11":$bln="XI/";break;
		case "12":$bln="XII/";break;
		default:
		  $bln = "BBBB/";
		}
		
		$th = mdate("%y", time());
		$thi = mdate("%Y", time());
		
		if($jenis == "polokal")
		{
			$rs = $this->db->where(array('param' => 'POL'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codel.$n."/".$bln.$th."/B";
		}
		else if($jenis == "poimport")
		{
			$rs = $this->db->where(array('param' => 'POI'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codei.$n."/".$bln."SKJF/".$thi;
		}
	}
	
	function GenCodeTerima($jenis){
		// Terima Barang dalam US : LPUS/SKJF/YY/MM/XXX, keterangan : YY = tahun, MM=bulan, XXX = nomor urut 1 tahun
		//Jika Terima Barang Lokal dalam Rp : 	
		//Jika jenis barang = bahan baku : LPID/SKJF/YY/MM/XXX	
		//Jika jenis barang = non bahan baku : LPCA/SKJF/YY/MM/XXX (CA= capsul dll)	
		//Jika Terima Barang Import : LPIM/SKJF/YY/MM/XXX
		
		$jenis 		= strtolower($jenis);
		$codeus 	= "LPUS/";
		$codelob 	= "LPID/";
		$codelonb 	= "LPCA/";
		$codeim 	= "LPIM/";
		
		$th = mdate("%y/%m/", time());
		
		// ------------------------------------------------ Reset Tiap Bulan untuk Generator -----------------	
		// $febs = ($d->th%100 > 0 ? ($d->th%4 == 0 ? ($d->th%400 > 0 ? 1:0):0):0)+28;
		$sekarang = mdate("%Y%m", time());
		
		$historyP = $this->db->where(array('param' => 'LPBV'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$s = new StdClass();
		$s->th = mdate("%Y", time());
		$s->bln = mdate("%m", time());
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahLPBV = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','LPBV')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','LPBV')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'LPBC'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahLPBC = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','LPBC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','LPBC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'LPBI'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahLPBI = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','LPBI')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','LPBI')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'LPBNC'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahLPBNC = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','LPBNC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','LPBNC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		// ---------------------------------------------------------------------------------------------------
		
		if($jenis == "valas")
		{
			$rs = $this->db->where(array('param' => 'LPBV'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codeus."SKJF/".$th.$n;
		}
		elseif($jenis == "capsul")
		{
			$rs = $this->db->where(array('param' => 'LPBC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codelonb."SKJF/".$th.$n;
		}
		elseif($jenis == "import")
		{
			$rs = $this->db->where(array('param' => 'LPBI'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codeim."SKJF/".$th.$n;
		}
		elseif($jenis == "bbaku")
		{
			$rs = $this->db->where(array('param' => 'LPBNC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codelob."SKJF/".$th.$n;
		}
		else
		{
			$rs = $this->db->where(array('param' => 'LPBNC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codelob."SKJF/".$th.$n;
		}
	}
	
	function GenCodeDO($jenis){
		//Jika DO Barang Lokal dalam US : DOUS/SKJF/YY/MM/XXX, keterangan : YY = tahun, MM=bulan, XXX = nomor urut 1 tahun	
		//Jika DO Barang Lokal dalam Rp : 	
		//Jika jenis barang = bahan baku : DOID/SKJF/YY/MM/XXX	
		//Jika jenis barang = non bahan baku : DOCA/SKJF/YY/MM/XXX (CA= capsul dll)	

		
		$jenis 		= strtolower($jenis);
		$codeus 	= "DOUS/";
		$codelob 	= "DOID/";
		$codelonb 	= "DOCA/";
		
		$th = mdate("%y/%m/", time());
		
		// ------------------------------------------------ Reset Tiap Bulan untuk Generator -----------------	
		// $febs = ($d->th%100 > 0 ? ($d->th%4 == 0 ? ($d->th%400 > 0 ? 1:0):0):0)+28;
		$sekarang = mdate("%Y%m", time());
		
		$historyP = $this->db->where(array('param' => 'DOV'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$s = new StdClass();
		$s->th = mdate("%Y", time());
		$s->bln = mdate("%m", time());
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahDOV = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','DOV')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','DOV')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'DOC'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahDOC = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','DOC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','DOC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'DONC'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahDONC = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','DONC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','DONC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		// ---------------------------------------------------------------------------------------------------
		
		if($jenis == "valas")
		{
			$rs = $this->db->where(array('param' => 'DOV'))->get('init')->result();
			$no = intval(trim($rs[0]->value));
			$n = substr("000".$no,-3);
			return $codeus."SKJF/".$th.$n;
		}
		elseif($jenis == "capsul")
		{
			$rs = $this->db->where(array('param' => 'DOC'))->get('init')->result();
			$no = intval(trim($rs[0]->value));
			$n = substr("000".$no,-3);
			return $codelonb."SKJF/".$th.$n;
		}
		elseif($jenis == "bbaku")
		{
			$rs = $this->db->where(array('param' => 'DONC'))->get('init')->result();
			$no = intval(trim($rs[0]->value));
			$n = substr("000".$no,-3);
			return $codelob."SKJF/".$th.$n;
		}
	}
	
	function GenCodeINV($jenis){
		//Jika invoice dalam US maupun Rp : INVC/SKJF/YY/MM/XXX, keterangan : YY = tahun, MM=bulan, XXX = nomor urut 1 tahun	
		//Jika invoice CAPSUL : INCA/SKJF/YY/MM/XXX, keterangan : YY = tahun, MM=bulan, XXX = nomor urut 1 tahun
		
		$jenis 	= strtolower($jenis);
		$code 	= "INVC/";
		$codeca	= "INCA/";
		
		$th = mdate("%y/%m/", time());
		
		// ------------------------------------------------ Reset Tiap Bulan untuk Generator -----------------	
		// $febs = ($d->th%100 > 0 ? ($d->th%4 == 0 ? ($d->th%400 > 0 ? 1:0):0):0)+28;
		$sekarang = mdate("%Y%m", time());
		
		$historyP = $this->db->where(array('param' => 'INVNC'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$s = new StdClass();
		$s->th = mdate("%Y", time());
		$s->bln = mdate("%m", time());
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahINVNC = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','INVNC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','INVNC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		
		$historyP = $this->db->where(array('param' => 'INVC'))->get('init')->result();
		$data = trim($historyP[0]->bulan);
		
		$d = new StdClass();
		$d->th = substr($data,0,4);
		$d->bln = substr($data,4,2);
		
		$ubahINVC = ($s->bln > $d->bln && $s->bln != $d->bln ? ($s->th >= $d->th ? $this->db->where('param','INVC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0):($s->th > $d->th ? $this->db->where('param','INVC')->update('init',array('bulan' => $sekarang, 'value' => 1)) :0));
		// ---------------------------------------------------------------------------------------------------
		
		if($jenis == "valas")
		{
			$rs = $this->db->where(array('param' => 'INVNC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $code."SKJF/".$th.$n;
		}
		elseif($jenis == "capsul")
		{
			$rs = $this->db->where(array('param' => 'INVC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $codeca."SKJF/".$th.$n;
		}
		elseif($jenis == "idr")
		{
			$rs = $this->db->where(array('param' => 'INVNC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $code."SKJF/".$th.$n;
		}
		else
		{
			$rs = $this->db->where(array('param' => 'INVNC'))->get('init')->result();
			$no = intval(trim($rs[0]->value))+1;
			$n = substr("000".$no,-3);
			return $code."SKJF/".$th.$n;
		}
	}
	
	
	function getMac(){
		exec("ipconfig /all", $output);
		$mac = array();
		$i=0;
		foreach($output as $line){
			if (preg_match("/(.*)Physical Address(.*)/", $line)){
				//$mac = $line;
				$mac[$i] = str_replace("Physical Address. . . . . . . . . :","",$line);
				$i++;
			}
		}
		
		return $mac;
	}

	function GenFile($path,$data){
		
		if ( ! write_file($path, $data))
		{
			return 0;
		}
		else
		{
			return 1;
		}
	}
	
	function InitFile(){		
		$item = array('info_id' => $this->encrypt->encode("No ID User"),
			  'info_nama' => $this->encrypt->encode("Username"),
			  'info_os' => $this->encrypt->encode("Operating System USe"),
			  'info_group' => $this->encrypt->encode("Info Group"),
			  'info_machine' => $this->encrypt->encode("Info Machine")
		);
		$items[] = $item;
		$data = json_encode($items);
		return $data;
	}
	
	function ReadFile($path){
		$isi = read_file($path);
		$item = json_decode($isi);
		$data = array('info_id' => $this->encrypt->decode($item[0]->info_id),
			  'info_nama' => $this->encrypt->decode($item[0]->info_nama),
			  'info_os' => $this->encrypt->decode($item[0]->info_os),
			  'info_group' => $this->encrypt->decode($item[0]->info_group),
			  'info_machine' => $this->encrypt->decode($item[0]->info_machine)
		);
		
		return $data;
	}
}

?>
